﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PocketApplicationsMVC.Views
{
    public partial class BoxNetForm : Form, IBoxNetView
    {
        string fileToUpload = "";

        public BoxNetForm()
        {
            InitializeComponent();
        }


        #region IBoxNetView Members

        #region methods
        public void LoggedIn(bool success)
        {
            this.BeginInvoke(new EventHandler(delegate
            {
                this.SuspendLayout();

                // invoke need for crossthread calls

                if (success == true)
                {
                    panel1.Enabled = false;
                    panel1.Visible = false;
                    pnBoxNetUI.Visible = true;
                }
                else
                {
                    panel1.Enabled = true;
                    btnLogin.Enabled = true;
                }

                this.ResumeLayout();
            }));


        }

        public void UpdateFolderContents(List<PocketLibrary.Storage.BoxNetAPITreeItem> contents)
        {
            lvBoxNetContent.BeginInvoke(new EventHandler(delegate
            {
                lvBoxNetContent.Items.Clear();
                for (int i = 0; i < contents.Count; i++)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = contents[i].Name;
                    item.SubItems.Add(contents[i].FileSize.ToString());
                    item.SubItems.Add(contents[i].ItemId);
                    item.SubItems.Add(contents[i].ItemType.ToString());
                    lvBoxNetContent.Items.Add(item);
                }
            }));
        }

        public void UpdateStatus(string text)
        {
            this.BeginInvoke(new EventHandler(delegate
            {
                if (text != "")
                {
                    sbStatus.Visible = true;
                }
                else
                {
                    sbStatus.Visible = false;
                }
                sbStatus.Text = text;

            }));

        }

        public void NotifyStatus(string caption, string text, bool critical)
        {
            //this.BeginInvoke(new EventHandler(delegate
            //{
            //    notStatus.Caption = caption;
            //    notStatus.Text = text;
            //    notStatus.Critical = critical;
            //    notStatus.Visible = true;
            //}));

            this.UpdateStatus(text);
        }

        public void UpdateFileProgress(int value)
        {
            this.BeginInvoke(new EventHandler(delegate
            {
                if (value > 0 && value != 100)
                {
                    pbFileProgress.Visible = true;
                    pbFileProgress.Value = value;
                }
                else
                {
                    pbFileProgress.Value = 0;
                    pbFileProgress.Visible = false;
                }
            }));
        }
        #endregion

        #region events
        public event EventHandler<BoxNetDownloadArgs> DownloadFile;

        public event EventHandler UploadFile;

        public event EventHandler<BoxNetUserArgs> OnUserLogin;

        public event EventHandler<BoxNetFileArgs> ListFolder;

        public event EventHandler ShowMenuForm;

        public event EventHandler UpdateFolder;

        public event EventHandler ListParentFolder;
        #endregion


        #region properties
        public string FileToUpload
        {
            get { return fileToUpload; }
        }

        #endregion
        #endregion

        private void authTextChanged(object sender, EventArgs e)
        {
            if (tbUserName.Text != "" && tbPassword.Text != "")
                btnLogin.Enabled = true;
            else
                btnLogin.Enabled = false;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            btnLogin.Enabled = false;
            if (this.OnUserLogin != null)
            {
                this.OnUserLogin(this, new BoxNetUserArgs(tbUserName.Text, tbPassword.Text));
            }
        }

        private void miMenu_Click(object sender, EventArgs e)
        {
            if (this.ShowMenuForm != null)
            {
                this.ShowMenuForm(this, null);
            }
        }

        private void miRefresh_Click(object sender, EventArgs e)
        {
            if (this.UpdateFolder != null)
            {
                this.UpdateFolder(this, null);
            }
        }

        private void lvBoxNetContent_ItemActivate(object sender, EventArgs e)
        {
            string _itemType = lvBoxNetContent.Items[lvBoxNetContent.SelectedIndices[0]].SubItems[3].Text;
            string _itemId = lvBoxNetContent.Items[lvBoxNetContent.SelectedIndices[0]].SubItems[2].Text;
            string _itemName = lvBoxNetContent.Items[lvBoxNetContent.SelectedIndices[0]].Text;

            if (_itemType == "Folder")
            {
                if (this.ListFolder != null)
                {
                    this.ListFolder(this, new BoxNetFileArgs(_itemId));
                }
            }
            else if (_itemType == "File")
            {
                if (MessageBox.Show("Ladataanko tiedosto?", "Tiedoston lataus",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.FileName = _itemName;
                    //sfd.Filter = _itemName.Substring(_itemName.Length - 3);
                    string _ext = _itemName.Substring(_itemName.Length - 3);
                    sfd.ShowDialog();
                    if (this.DownloadFile != null)
                    {
                        this.DownloadFile(this, new BoxNetDownloadArgs(_itemId, sfd.FileName + _ext));
                    }
                }
            }
            else if (_itemType == "Parent")
            {
                if (this.ListParentFolder != null)
                {
                    this.ListParentFolder(this, null);
                }
            }
        }

        private void miUpload_Click(object sender, EventArgs e)
        {
            if (this.UploadFile != null)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "All files|*.*|Pictures|*.jpg";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    this.fileToUpload = ofd.FileName;
                    this.UploadFile(this, null);
                }
            }

        }
    }
}